package com.go.where.manager.util;

import java.util.Map;

import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionContext;

public class WebTools {
	/**
	 * 非servlet API获取httpServletRequest值<br>
	 * 若有多个,只取第一个
	 * 
	 * @param name
	 * @return
	 */
	public static String getParameter(String name) {
		ActionContext ac = ActionContext.getContext();
		Map<String, Object> map = ac.getParameters();
		Object obj = map.get(name);
		String result = null;
		if (obj != null) {
			String[] arrStr = (String[]) obj;
			if (arrStr.length >= 1) {
				result = arrStr[0];
			}
		}
		return result;
	}

	/**
	 * 非servlet API获取httpServletRequest值<br>
	 * 可以获取多个值
	 * 
	 * @param name
	 * @return
	 */
	public static String[] getParameterValues(String name) {
		ActionContext ac = ActionContext.getContext();
		Map<String, Object> map = ac.getParameters();
		Object obj = map.get(name);
		String[] arrStr = null;
		if (obj != null) {
			arrStr = (String[]) obj;
		}
		return arrStr;
	}

	/**
	 * ajax返回数据
	 * 
	 * @param obj
	 * @throws Exception
	 */
	public static String writeAjax(Object obj) throws Exception {
		Gson gson = CommonUtil.gson();
		String json = "";
		if (obj != null) {
			json = gson.toJson(obj);
		}
		return json;
	}
}
